package com.example.dangerouscargodriver.controller;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Message;
import android.util.Log;

import com.baiju.netmanager.Http.HttpRequest;
import com.baiju.netmanager.Http.IHttpRequest;
import com.baiju.netmanager.Http.INetResponse;
import com.example.dangerouscargodriver.DangerousCargoDriverApplication;
import com.example.dangerouscargodriver.entry.LogInInfo;
import com.example.dangerouscargodriver.event.Event;
import com.example.dangerouscargodriver.event.LoadHttpResourceStatus;
import com.example.dangerouscargodriver.event.LoginStatus;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class AuthController extends Controller{

    public static final int STATUS_LOGIN = 0;
    public static final int STATUS_LOGOUT = 1;

    private int mLogStatus = STATUS_LOGOUT;
    private static final int MSG_ADD_LISTENER = 0;
    private static final int MSG_STATUS_CHANGE = 1;

    private static AuthController mInstance = null;

    private HttpRequest mHttp = null;
    private Context mContext = null;
    private String mPhone = null;
    private String mToken = null;

    private HttpResponseListener mHttpListener = null;
    private HashMap<java.lang.Class, IControllerEventListener> mControllerListener = null;

    public static AuthController Instance(){
        if(mInstance == null){
            mInstance = new AuthController(DangerousCargoDriverApplication.getAppContext());
        }

        return mInstance;
    }

    public AuthController(Context context){
        mContext = context;
        mControllerListener = new HashMap<>();
        mHttpListener = new HttpResponseListener();
        mHttp = HttpRequest.Instance(context);
        mHttp.attachResponseListener(mHttpListener, AuthController.class);
        mHttp.startUp();

        SharedPreferences sharedPreferences =
                DangerousCargoDriverApplication.getAppContext().getSharedPreferences(
                        "wsj_auth",
                        Context.MODE_PRIVATE);
        mToken = sharedPreferences.getString("token", null);

        if(checkLogin()){
            mLogStatus = STATUS_LOGIN;
        }
        else{
            mLogStatus = STATUS_LOGOUT;
        }
    }

    public boolean attachControllerListener(java.lang.Class object, IControllerEventListener listener){
        if(mControllerListener.containsKey(object)){
            return false;
        }
        else {
            mControllerListener.put(object, listener);
            return true;
        }
    }

    public boolean unAttachControllerListener(java.lang.Class object, IControllerEventListener listener){
        if(mControllerListener.containsKey(object) && mControllerListener.get(object) == listener){
            mControllerListener.remove(object);
            return true;
        }
        else {
            return false;
        }
    }

    public void logout(){
        setToken("");
    }

    public boolean checkLogin(){
        SharedPreferences sharedPreferences =
                DangerousCargoDriverApplication.getAppContext().getSharedPreferences(
                        "wsj_auth",
                        Context.MODE_PRIVATE);
        String token = sharedPreferences.getString("token", null);
        if(token != null && !token.equals("")){
            return true;
        }
        else{
            return false;
        }
    }

    public boolean setToken(String token){
        SharedPreferences sharedPreferences =
                DangerousCargoDriverApplication.getAppContext().getSharedPreferences("wsj_auth",
                        Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("token", token);
        editor.commit();

        mToken = token;

        if(checkLogin()){
            mLogStatus = STATUS_LOGIN;
        }
        else{
            mLogStatus = STATUS_LOGOUT;
        }

        Message message = new Message();
        message.what = MSG_STATUS_CHANGE;
        sendMessage(message);

        return true;
    }

    public String getToken(){
        return mToken;
    }

    @Override
    public void handleControllerMessage(Message msg) {
        switch (msg.what){
            case MSG_ADD_LISTENER:
                LoginStatus status = new LoginStatus();
                status.setWhat(mLogStatus);
                mControllerListener.get(msg.obj).onEvent(Event.EVENT_AUTH, status.toJson());
                break;
            case MSG_STATUS_CHANGE:
                LoginStatus loginStatus = new LoginStatus();
                loginStatus.setWhat(mLogStatus);
                for (java.lang.Class key : mControllerListener.keySet()) {
                    mControllerListener.get(key).onEvent(Event.EVENT_AUTH, loginStatus.toJson());
                }
                break;
            default:
                break;
        }
    }

    private class HttpResponseListener implements INetResponse {

        private static final String TAG = "HttpResponseListener";

        @Override
        public void onResponse(int actionId, String response) {
            try {
                JSONObject res = new JSONObject(response);
                Gson gson = new Gson();
                Log.d(TAG, "onResponse: " + response);

            }
            catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onErrorResponse(int actionId, String response) {

        }
    }
}
